package com.tender.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tender.SysMsgInfoBizService;
import com.tender.page.PageDTO;
import com.tender.dto.SysMsgInfoDTO;
import com.tender.condition.SysMsgInfoCondition;
import com.tender.entity.SysMsgInfoEntity;
import com.tender.service.SysMsgInfoService;
import com.tender.utils.LoginUser;
import com.tender.utils.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;

@Service
public class SysMsgInfoBizServiceImpl implements SysMsgInfoBizService {

    @Autowired
    private SysMsgInfoService sysMsgInfoService;

    @Override
    public SysMsgInfoDTO detail(Long id) {
        Assert.notNull(id, "消息主键id不能为空");
        SysMsgInfoEntity sysMsgInfoEntity = sysMsgInfoService.detail(id);

        return null != sysMsgInfoEntity ? BeanUtil.copyProperties(sysMsgInfoEntity, SysMsgInfoDTO.class) : null;
    }

    @Override
    public PageDTO<SysMsgInfoDTO> pageListManage(SysMsgInfoCondition condition) {
        Assert.notNull(condition, "系统消息分页查询入参不能为空");
        IPage<SysMsgInfoEntity> iPage = sysMsgInfoService.pageList(condition);

        return PageDTO.of(iPage, item -> BeanUtil.copyProperties(item, SysMsgInfoDTO.class));
    }

    @Override
    public PageDTO<SysMsgInfoDTO> pageListUser(SysMsgInfoCondition condition) {
        IPage<SysMsgInfoEntity> iPage = sysMsgInfoService.pageListUser(condition);

        return PageDTO.of(iPage, item -> BeanUtil.copyProperties(item, SysMsgInfoDTO.class));
    }

    @Override
    public Boolean online(Long id) {
        return sysMsgInfoService.online(id);
    }

    @Override
    public Boolean offline(Long id) {
        return sysMsgInfoService.offline(id);
    }

    @Override
    public Boolean modify(SysMsgInfoDTO param) {
        LoginUser userInfo = UserProvider.getUserInfo();
        Date now = new Date();
        Assert.notNull(param.getId(), "更新系统消息，消息id不能为空");
        SysMsgInfoEntity detail = sysMsgInfoService.detail(param.getId());
        if (null != detail) {
            SysMsgInfoEntity db = BeanUtil.copyProperties(detail, SysMsgInfoEntity.class);
            db.setContent(param.getContent());
            db.setResume(param.getResume());
            db.setTitle(param.getTitle());
            db.setClassification(param.getClassification());
            db.setUpdateUserId(userInfo.getUserId());
            db.setGmtUpdate(now);

            return sysMsgInfoService.updateById(db);
        }

        return false;
    }

    @Override
    public Boolean remove(Long id) {
        return sysMsgInfoService.removeById(id);
    }
}
